package com.mall.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.pageResult.PageResult;
import com.mall.pojo.UmsResource;
import com.mall.service.UmsResourceService;
import com.mall.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/resource")
public class UmsResourceController {

    @Autowired
    private UmsResourceService umsResourceService;

    //分页模糊查询后台资源
    @GetMapping("/list")
    public Result<PageResult<UmsResource>> list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "categoryId", required = false) Long categoryId,
                                                @RequestParam(value = "nameKeyword", required = false) String nameKeyword,
                                                @RequestParam(value = "urlKeyword", required = false) String urlKeyword) {
        Page<UmsResource> page = umsResourceService.list(pageNum, pageSize, categoryId, nameKeyword, urlKeyword);
        PageResult<UmsResource> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        if (CollUtil.isEmpty(page.getRecords())) {
            pageResult.setList(new ArrayList<>());
            return Result.success(pageResult, "操作成功");
        }
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        return Result.success(pageResult, "操作成功");
    }

    //查询所有后台资源
    @GetMapping("/listAll")
    public Result<List<UmsResource>> listAll() {
        List<UmsResource> list = umsResourceService.listAll();
        return Result.success(list, "操作成功");
    }

    //添加后台资源
    @PostMapping("/create")
    public Result create(@RequestBody UmsResource umsResource) {
        umsResourceService.create(umsResource);
        return Result.success(null, "操作成功");
    }

    //根据ID删除后台资源
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Long id) {
        umsResourceService.delete(id);
        return Result.success(null, "操作成功");
    }

    //修改后台资源
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Long id,
                         @RequestBody UmsResource umsResource) {
        umsResourceService.update(id, umsResource);
        return Result.success(null, "操作成功");
    }

    //根据ID获取资源详情
    @GetMapping("/{id}")
    public Result<UmsResource> selectById(@PathVariable("id") Long id) {
        UmsResource umsResource = umsResourceService.selectById(id);
        return Result.success(umsResource, "操作成功");
    }
}
